<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/wellcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 添加按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <!-- 角色列表区域 -->
      <el-table :data="roleslist" stripe style="width: 100%" border>
        <!-- 展开列 -->
        <el-table-column  type="expand">
          <template v-slot="slot">
            <el-row :class="['bdbottom', index1 === 0?'bdtop':'','vcenter']" v-for="(item1,index1) in slot.row.children" :key="item1.id">
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removeRole(slot.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <!-- 二级权限 -->
                <el-row :class="[index2 === 0 ?'':'bdtop','vcenter']"  v-for="(item2,index2) in item1.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag type="success" closable @close="removeRole(slot.row,item2.id)">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <!-- 三级权限 -->
                    <el-tag 
                    :class="index3 === 0 ?'':'bdtop'" v-for="(item3,index3) in item2.children" :key="item3.id"
                    type="warning" closable @close="removeRole(slot.row,item3.id)">
                      {{item3.authName}}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称">
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述">
        </el-table-column>
        <el-table-column label="操作" width="300px">
            <template v-slot="slot1">
                <el-button size="mini" type="primary" icon="el-icon-edit" >编辑</el-button>
                <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
                <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetrights(slot1.row)">分配权限</el-button>
            </template>
        </el-table-column>
    </el-table>
    </el-card>

    <!-- 分配权限弹出框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="rightsDialogVisible"
      width="50%"
      center @close="dialogClose" :modal-append-to-body="false"
      :close-on-click-modal="false">
      <!-- 树形控制组件 -->
      <el-tree ref="treeRef" :data="rightsList" :props="treeProps"
      :accordion="true" show-checkbox node-key="id" 
      :default-expand-all="true" :default-checked-keys="defaultKeys"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightsDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="alltoRights()">确 定</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>
  export default {
    name:'Roles',
    data(){
      return{
        roleslist:[],
        rightsDialogVisible:false,
        // 所有权限数据
        rightsList:[],
        // 树形属性
        treeProps:{
          children: 'children',
          label: 'authName'
        },
        // 默认选中的权限
        defaultKeys:[],
        roleId:''
      }
    },
    created(){
      this.getroleList();
    },
    methods:{
      async getroleList(){
        const res = await this.axios.get('roles');
        // console.log(res);
        const {data} = res.data;
        const {status} = res.data.meta;
        if(status !== 200) return this.$message.error('获取角色列表失败~')
        this.roleslist = data;
      },
      async removeRole(role,rightId){
        const confirmRes = await  this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err);

        // console.log(confirmRes);
        if(confirmRes !== 'confirm'){
          return this.$message.info('取消了删除~')
        }
        // 删除权限
        const res = await this.axios.delete(`roles/${role.id}/rights/${rightId}`)
        // console.log(res);
        if(res.data.meta.status !== 200){
          return this.$message.error('删除权限失败！')
        }
        // 删除后重新渲染
        role.children = res.data.data;
      },
      async showSetrights(role){
        this.roleId = role.id;
        const {data:res} = await this.axios.get('rights/tree');
        if(res.meta.status !== 200){
          return this.$message.error('获取权限失败了！')
        }
        this.rightsList = res.data;
        // console.log(this.rightsList);
        this.getLeafKeys(role,this.defaultKeys)
        this.rightsDialogVisible = true;
      },
      // 递归查询结点的三级权限
      getLeafKeys(node,arr){
        if(!node.children){
          return arr.push(node.id)
        }
        node.children.forEach(item => {
          this.getLeafKeys(item,arr)
        });
      },
      // 对话框关闭事件
      dialogClose(){
        this.defaultKeys = [];
      },
      // 分配权限
      async alltoRights(){
        const keys = [
          ...this.$refs.treeRef.getCheckedKeys(),
          ...this.$refs.treeRef.getHalfCheckedKeys()
        ];
        // console.log(keys);
        const idstr = keys.join(',');
        const {data: res} = await  this.axios.post(`roles/${this.roleId}/rights`,{rids:idstr});
        
        if(res.meta.status !==200){
          return this.$message.error('分配权限失败了~');
        }

        this.$message.success('分配权限成功~');
        this.getroleList();
        this.rightsDialogVisible = false;
      }
    }
  };
</script>

<style>
  .el-aside {
    color: #333;
  }
  .el-tag{
    margin: 10px;
  }
  .bdtop{
    border-top: 1px solid #eeee;
  }
  .bdbottom{
    border-bottom: 1px solid #eeee;
  }
  .vcenter{
    display: flex;
    align-items: center;
  }
</style>